/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import server.MailServer;

/**
 * CrearServer representa la interfaz grafica para crear servidores, a los cuales se registraran usuario
 * @author Nick
 */
public class CrearServer extends javax.swing.JFrame {
    private DefaultTableModel modeloServer;
    /**
     * Constructor de la clase CrearServer.
     * Creates new form CrearServer
     */
    public CrearServer() {
        initComponents();
        this.setSize(650, 400);
        this.setLocationRelativeTo(null);
        //cargando la tabla con todos los servidores ya creados.
        modeloServer = new DefaultTableModel(new Object[]{"Nombre","Usuario Registrado"}, 0);
        jTable1.setModel(modeloServer);
        actualizarTablaServidores();
    }
    /**
     * Metodo que obtiene la lista de servidores y las carga en la tabla de servidores.
     */
    public void actualizarTablaServidores(){
        modeloServer = new DefaultTableModel(new Object[]{"Nombre","Usuario Registrado"}, 0);
        jTable1.setModel(modeloServer);
        int cantidadServers = Login.servidores.size();        
        for (int i = 0; i < cantidadServers; i++) {
            MailServer serverAux = Login.servidores.get(i);
            Object[] datos = new Object[2];
            datos[0] = serverAux;
            datos[1] = serverAux.cantidadUsuarioRegistrados();
            modeloServer.addRow(datos);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jtxtNombreServer = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 3, 18)); // NOI18N
        jLabel1.setText("Crear Server");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(160, 10, 150, 30);

        jPanel1.setBackground(new java.awt.Color(0, 102, 153));
        jPanel1.setLayout(null);

        jButton1.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
        jButton1.setText("Crear");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(370, 30, 90, 30);
        jPanel1.add(jtxtNombreServer);
        jtxtNombreServer.setBounds(100, 30, 260, 30);

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
        jLabel2.setText("Nombre");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(30, 30, 70, 30);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(20, 80, 452, 100);

        jButton2.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
        jButton2.setText("Eliminar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);
        jButton2.setBounds(190, 200, 110, 40);

        jButton3.setFont(new java.awt.Font("Comic Sans MS", 1, 14)); // NOI18N
        jButton3.setText("Ver Usuarios");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(340, 200, 130, 40);

        jButton4.setBackground(new java.awt.Color(204, 204, 204));
        jButton4.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
        jButton4.setText("<-- Atras");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4);
        jButton4.setBounds(20, 200, 120, 40);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 50, 490, 260);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String nameServer = jtxtNombreServer.getText();
        if (nameServer.equals("")) {
            JOptionPane.showMessageDialog(null, "Error. Debe ingresar un nombre para crear el servidor.");
        }
        MailServer nuevoServer = new MailServer(nameServer);
        Login.servidores.add(nuevoServer);
        actualizarTablaServidores();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int filaSeleccionada = jTable1.getSelectedRow();
        if (filaSeleccionada != -1) {
            MailServer seleccionado = (MailServer) modeloServer.getValueAt(filaSeleccionada, 0);
            Login.formularioListadoUsuarios = new ListarUsuario(seleccionado);
            this.setVisible(false);
            Login.formularioListadoUsuarios.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(null, "Debe seleccionar un servidor para ver sus usuarios");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        Login.getInstance().setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int filaSeleccionada = jTable1.getSelectedRow();
        if (filaSeleccionada == -1) {
            JOptionPane.showMessageDialog(null,"Debe seleccionar un servidor a eliminar.");
            return;
        }
        MailServer serverDelete = (MailServer)modeloServer.getValueAt(filaSeleccionada, 0);
        Login.servidores.remove(serverDelete);
        actualizarTablaServidores();
        Login.getInstance().actualizarVista();
    }//GEN-LAST:event_jButton2ActionPerformed

  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jtxtNombreServer;
    // End of variables declaration//GEN-END:variables
}
